package com.xframe.xdal.core.annotation;

import com.xframe.xdal.core.autofill.IAutoFillBean;
import com.xframe.xdal.core.constant.AutoFillType;
import com.xframe.xdal.core.constant.RunOpportunity;
import lombok.Data;

import java.lang.reflect.Field;

@Data
public class AutoFillBean implements IAutoFillBean{
    /**
     * 自动填充类型
     */
    private AutoFillType autoFillType = AutoFillType.Value;
    /**
     * 默认值
     */
    private String defVal;
    /**
     * 附加参数
     */
    private String args;
    /**
     * 自定义填充的类型
     */
    private String type;
    /**
     * 是否强制覆盖值，写入时候是否判断是否为空值,
     */
    private Boolean overwrite;
    /**
     * 调用的时机 C R U
     */
    private Integer operateMark = 0;
    /**
     * 排序
     */
    private Integer order = 0;

    /**
     * 字段类型
     */
    private Class<?> fieldType;


    public boolean hasRole(RunOpportunity runOpportunity){
        return RunOpportunity.exists(this.operateMark,runOpportunity);
    }

    /**
     * 将注解对象转换成AutoFillBean
     * @param autoFill 注解
     * @return AutoFillBean对象
     */
    public static AutoFillBean valueOf(AutoFill autoFill, Field field){
        AutoFillBean autoFillBean = new AutoFillBean();
        autoFillBean.autoFillType = autoFill.value();
        autoFillBean.operateMark = autoFill.operateMark();
        autoFillBean.args = autoFill.args();
        autoFillBean.order = autoFill.order();
        autoFillBean.type = autoFill.type();
        autoFillBean.defVal = autoFill.defVal();
        autoFillBean.overwrite = autoFill.overwrite();
        autoFillBean.fieldType = field.getType();
        return autoFillBean;
    }

    public static AutoFillBean valueOf(AutoFillType autoFillType, String defVal, String args, String type, boolean overwrite, int order, int operateMark){
        AutoFillBean autoFillBean = new AutoFillBean();
        autoFillBean.autoFillType = autoFillType;
        autoFillBean.defVal = defVal;
        autoFillBean.args = args;

        autoFillBean.type = type;
        autoFillBean.order = order;
        autoFillBean.overwrite = overwrite;
        autoFillBean.operateMark = operateMark;
        return autoFillBean;
    }

    public static AutoFillBean valueOf(AutoFillType autoFillType, String defVal, String args, String type){
        return valueOf(autoFillType,defVal,args,type,false,0,1);
    }

    public static AutoFillBean valueOf(AutoFillType autoFillType){
        return valueOf(autoFillType,"","","",false,0,1);
    }

    public static AutoFillBean valueOf(AutoFillType autoFillType, String defVal){
        return valueOf(autoFillType,defVal,"","",false,0,1);
    }

    @Override
    public int compareTo(IAutoFillBean bean) {
        return this.order - bean.getOrder();
    }




}
